package com.java.core.script;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.List;

/**
 * @Author YongQiang
 * @Date 2021/10/9 17:21
 * @Version 1.0
 */
public class EngineManager {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        for (ScriptEngineFactory engineFactory : engineFactories) {
            System.out.println("name:"+engineFactory.getEngineName() + ",  mimeTypes:"+engineFactory.getMimeTypes() + ",  extension:"+engineFactory.getExtensions());
            // 多个线程中并发执行脚本是否安全
            System.out.println(engineFactory.getParameter("THREADING"));
        }

        ScriptEngine scriptEngine = scriptEngineManager.getEngineByExtension("js");
        scriptEngine.eval("n=5");
        Object eval = scriptEngine.eval("n+=3");
        System.out.println(eval);
    }
}
